/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.potomak.patientmanager.gui;

import net.quies.math.plot.Graph;
import net.quies.math.plot.ChartStyle;
import net.quies.math.plot.InteractiveGraph;
import net.quies.math.plot.Function;
import java.awt.Color;
import java.math.BigDecimal;
import javax.swing.JDialog;
import javax.swing.JFrame;
import net.quies.math.plot.ChartType;
import net.quies.math.plot.XAxis;
import net.quies.math.plot.YAxis;
import org.potomak.patientmanager.beans.model.CheckUpBean;

/**
 *
 * @author mobile
 */
final class ChartDialog extends JDialog {
    
    private CheckUpBean[] functionPoints;

    public ChartDialog(JFrame parent, CheckUpBean[] function) {
        super(parent);
        this.functionPoints = function;
        initGraph();
        initFunctions();
        getContentPane().add(graph);
    }
    
    public void refresh() {
        graph.render();
        graph.repaint();
    }

    private void initGraph() {
        graph.getYAxis().setZigZaginess(BigDecimal.valueOf(7L, 1));
        graph.getXAxis().setZigZaginess(BigDecimal.valueOf(7L, 1));
        graph.setBackground(Color.WHITE);
    }

    private void initFunctions() {
        ChartStyle style1 = new ChartStyle();
        style1.setUpperLimitEnabled(true);
        style1.setLowerLimitEnabled(true);
        style1.setPaint(Color.BLUE);
        graph.addFunction(function, style1);
        ChartStyle style2 = new ChartStyle();
        style2.setPaint(Color.RED);
        style2.setUpperLimitEnabled(true);
        graph.addFunction(initialWeightConstant, style2);
        
        for(int i = 0; i < functionPoints.length; i++) {
            function.addPoint(new BigDecimal((functionPoints[i].getDate().getTime()-functionPoints[0].getDate().getTime())/86400000), new BigDecimal(functionPoints[i].getWeight()));
            System.out.println(i+" pair: ("+new BigDecimal((functionPoints[i].getDate().getTime()-functionPoints[0].getDate().getTime())/86400000)+","+new BigDecimal(functionPoints[i].getWeight())+")");
        }
        
        initialWeightConstant.addPoint(new BigDecimal((functionPoints[0].getDate().getTime()-functionPoints[0].getDate().getTime())/86400000),new BigDecimal(((Contacts)this.getParent()).getSelectedPatient().getWeight()));
        initialWeightConstant.addPoint(new BigDecimal((functionPoints[functionPoints.length-1].getDate().getTime()-functionPoints[0].getDate().getTime())/86400000),new BigDecimal(((Contacts)this.getParent()).getSelectedPatient().getWeight()));
    }

    private final Graph graph = new InteractiveGraph(new XAxis("Time"),new YAxis("Weight"));
    private final Function function = new Function("weight function");
    private final Function initialWeightConstant = new Function("initial weight");
}
